﻿using System;
using System.Collections.Generic;
using System.Text;
using DotConferenceCore.GestionParticipant;
using DotConferenceSql;

namespace DataLayer
{
    class ParticipantDataManager
    {

        private static ParticipantDataManager _instance;

        public static ParticipantDataManager Instance
        {
            get
            {
                if( _instance == null )
                    _instance = new ParticipantDataManager();
                return _instance;
            }
        }

        public bool Save(Participant participant)
        {
            bool resultat = false;
            
            using( var query = new SqlQuery() )
            {
                List<DotConferenceSql.QueryFieldValue> fieldValues = new List<QueryFieldValue>();

                
                fieldValues.Add(new QueryFieldValue { field = "Titre", value = participant.Titre});
                fieldValues.Add(new QueryFieldValue { field = "Nom", value = participant.Nom});
                fieldValues.Add(new QueryFieldValue { field = "Prenom", value = participant.Prenom});
                fieldValues.Add(new QueryFieldValue { field = "Statut", value = participant.Statut });
                fieldValues.Add(new QueryFieldValue { field = "Organisme", value = participant.Organisme });
                fieldValues.Add(new QueryFieldValue { field = "Telephone", value = participant.Telephone });
                fieldValues.Add(new QueryFieldValue { field = "Adresse", value = participant.Adresse });
                fieldValues.Add(new QueryFieldValue { field = "Courriel", value = participant.Courriel });
                fieldValues.Add(new QueryFieldValue { field = "Identifiant", value = participant.Identifiant });
                fieldValues.Add(new QueryFieldValue { field = "MotDePasse", value = participant.MotDePasse });
                fieldValues.Add(new QueryFieldValue { field = "TypeParticipant", value =(int) participant.Type });

                if (typeof(Evaluateur) == participant.GetType())
                {
                    fieldValues.Add(new QueryFieldValue
                                        {field = "Specialite", value = ((Evaluateur) participant).Specialite});
                }

                
                if (participant.NumeroParticipant < 1)
                {
                    InsertIntoQueryElement queryInsert = new InsertIntoQueryElement();

                    foreach (DotConferenceSql.QueryFieldValue fv in fieldValues)
                    {
                        queryInsert.Add(fv.field, fv.value);
                    }

                    queryInsert.Table = "Participant";

                    query.Elements.Add(queryInsert);
                }   
                else
                {
                    UpdateQueryElement queryUpdate = new UpdateQueryElement();

                    foreach (DotConferenceSql.QueryFieldValue fv in fieldValues)
                    {
                        queryUpdate.Add(fv.field, fv.value);
                    }

                    queryUpdate.Table = "Participant";

                    query.Elements.Add(queryUpdate);

                    query.Elements.Add(new CustomWhereStatement(string.Format("NumeroParticipant={0}", participant.NumeroParticipant)));
                }


                var command = query.BuildSqlCommand();

                resultat = command.ExecuteNonQuery() > 0;
            

                if (participant.NumeroParticipant < 1 && resultat)
                {

                    command.CommandText = "SELECT @@IDENTITY";

                    participant.NumeroParticipant = Convert.ToInt32(command.ExecuteScalar());
                }
            }

            return resultat;
         
        }
        public IList<Participant> GetBy(string whereClause)
        {

            List<Participant> participants = new List<Participant>();

            using (var query = new DotConferenceSql.SqlQuery())
            {

                var selectPart = new DotConferenceSql.SelectQueryElement();

                selectPart.Add("*");
                

                query.Elements.Add(selectPart);
                query.Elements.Add(new DotConferenceSql.FromTableQueryElement("Participant"));

                query.Elements.Add(new DotConferenceSql.CustomWhereStatement(whereClause));

                var selectCommand = query.BuildSqlCommand();

                var dReader = selectCommand.ExecuteReader();

                if (dReader != null)
                {
                    while (dReader.Read())
                    {
                        participants.Add(CreateParticipant(dReader));
                    }
                    
                    dReader.Close();
                }
            }

            return participants;
        }

        protected Participant CreateParticipant(System.Data.IDataReader dataReader)
        {
            Participant monParticipant = null;

            Participant.TypeParticipant type = (Participant.TypeParticipant) Convert.ToInt32(dataReader["TypeParticipant"]);

            switch(type)
            {
                case Participant.TypeParticipant.Auteur:
                    monParticipant = new Auteur();
                    break;

                case Participant.TypeParticipant.Evaluateur:
                    monParticipant = new Evaluateur(dataReader["Specialite"].ToString());
                    break;

                case Participant.TypeParticipant.Invite:
                    monParticipant = new Invite();
                    break;

                case Participant.TypeParticipant.Organisateur:
                    monParticipant = new Organisateur();
                    break;

                case Participant.TypeParticipant.Secretaire:
                    monParticipant = new Secretaire();
                    break;
                
                default:
                    monParticipant = new Participant();
                    break;
            }


            monParticipant.Titre = dataReader["Titre"].ToString();
            monParticipant.Nom = dataReader["Nom"].ToString();
            monParticipant.Prenom = dataReader["Prenom"].ToString();
            monParticipant.Statut = dataReader["Statut"].ToString();
            monParticipant.Organisme = dataReader["Organisme"].ToString();
            monParticipant.Telephone = dataReader["Telephone"].ToString();
            monParticipant.Adresse = dataReader["Adresse"].ToString();
            monParticipant.Courriel = dataReader["Courriel"].ToString();
            monParticipant.Identifiant = dataReader["Identifiant"].ToString();
            monParticipant.MotDePasse = dataReader["MotDePasse"].ToString();

            return monParticipant;
            
        }
    }


}

